﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(ui->comboBox, SIGNAL(currentIndexChanged(QString)),
               this, SLOT(serial_list_comboBox_changed(QString)));

    MSerialPortList *serial_lists = MSerialPortList::instance();
    connect(serial_lists, &MSerialPortList::new_serial_port_update,
            this, [=](const QSerialPortInfoList &lists) {
        disconnect(ui->comboBox, SIGNAL(currentIndexChanged(QString)),
                   this, SLOT(serial_list_comboBox_changed(QString)));
        ui->comboBox->clear();
        foreach(QSerialPortInfo serial, lists) {
            ui->comboBox->addItem(serial.portName() + " (" + serial.description() + ")");
        }
        const QSerialPortInfo info = serial_lists->get_select_serial();
        ui->comboBox->setCurrentText(info.portName() + " (" + info.description() + ")");
        connect(ui->comboBox, SIGNAL(currentIndexChanged(QString)),
                   this, SLOT(serial_list_comboBox_changed(QString)));
    });

    // test.start();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{
    qDebug() << "serial port name = " << ui->comboBox->currentText().split(" ").at(0);
}

void MainWindow::serial_list_comboBox_changed(const QString &arg1)
{
    MSerialPortList *serial_lists = MSerialPortList::instance();
    serial_lists->set_select_serial(arg1.split(" ").at(0));
}
